<?php

include "global.php"; // Get Configuration
getsettings();

$company_dropdown = "<select name='company' id='company'>";
$query = $DB_site->query("SELECT id,company_name FROM companies ORDER BY company_name ASC");
while (list($cid,$company_name) = $DB_site->fetch_array($query))
{
	$company_dropdown.= "<option value='$cid'>$company_name</option>";
}
$company_dropdown.= "</select>";

if (isset($_POST["doaction"]) && $_POST["doaction"] == "register")
{
	$username = sql_safe($_POST["username"]);
	$password = sql_safe($_POST["password"]);
	$rpassword = sql_safe($_POST["rpassword"]);
	$fname = sql_safe($_POST["fname"]);
	$lname = sql_safe($_POST["lname"]);
	$company = sql_safe($_POST["company"]);
	$other_company = sql_safe($_POST["other_company"]);
	$title = sql_safe($_POST["title"]);
	$industry = sql_safe($_POST["industry"]);
	$phone = sql_safe($_POST["phone"]);
	$address = sql_safe($_POST["address"]);
	$details = sql_safe($_POST["details"]);
	
	$flag = 0;
	$register_msg = "Following Error(s) Occured during  registration:<br />";

	// Check password length
	if (strlen($password) < 5)
	{
		$register_msg .= "Password too short, Please use at least 5 character Password<br />";
		$flag = 1;
	}

	// Compare password and repeat password
	if ($password != $rpassword)
	{
		$register_msg .= " - Password and Confirm Password don't match. Please try again.<br />";
		$flag = 1;
	}
	
	// Filter out Bad Emails
	if (!check_email_address($username))
	{
		$register_msg .= "Invalid address syntax.<br />";
		$flag = 1;
	}
	
	if ($other_company != "") // Other company given
	{
		$company_name = trim($other_company);
		if ($company_name != "") // add other to database if not there
		{
			list($check) = $DB_site->query_first("SELECT count(*) FROM companies WHERE company_name='$company_name'");
			if ($check <= 0)
			{
				$result = $DB_site->query("INSERT INTO companies (company_name,submit_time) VALUES('$company_name',now())");
			}
		}
	}
	else
	{
		list($company_name) = $DB_site->query_first("SELECT company_name FROM companies WHERE id='$company'");
	}
	if ($company_name == "")
	{
		$register_msg .= "Please fill in the Company Name<br />";
		$flag = 1;
	}
	
	list($count) = $DB_site->query_first("SELECT count(*) FROM volunteers where email='$email'");
	if ($count > 0)
	{
		$register_msg.= "Email already exists in the database. Please enter another Email.<br />";
		$flag = 1;
	}
	
	if ($flag == 0)
	{
		srand((double)microtime()*1000000);
		$activation_code = md5(uniqid(rand()));
		$activation_code = substr($activation_code,2,10);
		$activation_code = strtoupper($activation_code);

		if ($activation_required)
		{
			$result = $DB_site->query("INSERT INTO volunteers (password,email,fname,lname,phone,company,title,industry,address,details,activation_code,account_status,submit_time,comments)
											VALUES('$password','$username','$fname','$lname','$phone','$company_name','$title','$industry','$address','$details','$activation_code','Unconfirmed',now(),'')
								");

			eval("\$message = \"".gettemplate('new_volunteers_account_email')."\";");

			send_email($do_not_reply_email,$email,"Volunteer Match Volunteer Registration – Your Account Needs to Be Activated",$message);
			$msg =  "Registration Mail has been sent to your email address containing the Login Information. <br /><hr size=\"1\"><a href=\"$website_home\">Back to Home</a>";
			echo $msg;
			exit;		
		}
		else // No activation required, so just login and get going
		{
			$result = $DB_site->query("INSERT INTO volunteers (password,email,fname,lname,phone,company,title,industry,address,details,activation_code,account_status,submit_time,comments)
											VALUES('$password','$username','$fname','$lname','$phone','$company_name','$title','$industry','$address','$details','$activation_code','Active',now(),'')
								");
			list($member_id) = $DB_site->query_first("SELECT id from volunteers where email='$username' AND password='$password' AND account_status ='Active'");

			srand((double)microtime()*1000000);
			$session_id = md5(uniqid(rand()));
			$session_id = addslashes($session_id);
			$result = $DB_site->query("INSERT INTO sessions (member_id,email,member_type,session_id,logout_time) 
												 VALUES('$member_id','$username','volunteer','$session_id',DATE_ADD(now(), INTERVAL $session_logout MINUTE))
						  ");
			setcookie("sess",$session_id,time()+$session_logout*60);
			Header("Location: $website_volunteers_overview?sess=$session_id");
			exit;
		}
	}
}
?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <link rel="stylesheet" type="text/css" href="eweek.css" />
    </head>
    <body>
        <?php require("header.php"); ?>
		<div> <label><?php echo($registration_msg); ?></label></div>
        <form method="POST">
		<input type="hidden" name="doaction" value="register">
        <section id="left">
          <fieldset id="reg1" class="inblk">
		
            <legend>Contact Information</legend>
              <div><label for="fname"> First Name:* </label>
               <input type="text" id="fname" name="fname" required="required"/>
              </div>
                   <div><label for="lname"> Last Name:* </label>
                    <input type="text" id="lname" name="lname" required="required"/>
                  </div>
                  <div>
                    <label for="company"> Company:* </label>
					<?php echo ($company_dropdown); ?>
                    (or former company) 
                  </div>
                   <div>
                        <label for="other_company"> Other: </label>
                        <input type="text" id="other_company" name="other_company"/>
                        (if company not listed above)
                  </div>
             <div>
                    <label for="address"> Address: </label>
                    <input id="address" name="address" type="text"/>
                  </div> 
                  <div>
                    <label for="phone"> Phone: </label>
                    <input type="text" id="phone" name="phone"/>
                  </div> 
                  
            </fieldset>
                           
            <fieldset id="reg3" class="inblk">
                <legend>Create username</legend>
                    <div>
                       <label for="username" class="uname"> Email:* </label>
                       <input id="username" name="username" required type="text" placeholder="mymail@mail.com"/> 
                        
                    </div>
                    <div>
                       <label for="password" class="passwd"> Password:* </label>
                      <input id="password" name="password" required type="password" placeholder="eg. X8df!90EO" /> 
                    </div>
                    <div>
                       <label for="password" class="passwd"> Confirm Password:* </label>
                      <input id="password" name="rpassword" required type="password" placeholder="eg. X8df!90EO" />
                    </div>
                  
                   
                       
            </fieldset> 
                <div id="register">
                    <input type="submit" value="Register" /> <input type="submit" value="Cancel" />
                    <label>Already registered? </label> <a href="indexnew.php">Login here</a>
                </div>
        </section> 
        <section id="right" >
            
                <fieldset class="inblk">
                    <legend>Other information</legend>
                <div>
                        <label for="title"> Title: </label>
                        <input type="text" id="title" name="title"/>
                </div>
                
                    <div>
                       <label for="industry"> Industry: </label>
                       <input id="industry" name="industry" type="text"/> 
                        
                    </div>
                   
                    <div>
                       <label for="details"> Background : </label>
                      <textArea rows="5" id="details" name="details" > 
                      </textArea>
                    </div>   
                    
				</fieldset>
				<?php echo ($$register_msg); ?>	
        </section>
       </form>
   
       <?php require("footer.php"); ?>
    </body>
</html>
